package com.example.tushu.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.tushu.Pojo.JsonResult;
import com.example.tushu.Pojo.Loan;
import com.example.tushu.Service.LoanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class LoanController {
    @Autowired
    LoanService loanService;

    /**
     * 分页查询
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getAll")
    public JsonResult getAll(Long current,Long size){
        return new JsonResult(loanService.page(current,size));
    }

    /**
     * 借阅和规范，
     * @param loan
     * @return
     */
    @GetMapping("/changestatus")
    public JsonResult changeStatus(Loan loan){
//        System.out.println("loan = " + loan);
        QueryWrapper<Loan> wrapper=new QueryWrapper<>();
        wrapper.eq("id",loan.getId());
        if (loanService.count(wrapper)==1){
            UpdateWrapper<Loan> updateWrapper=new UpdateWrapper<>();
            updateWrapper.set("loan_status",loan.getStatus()).eq("id",loan.getId());
            updateWrapper.set("loan_user_id",loan.getUserId());
            if (!loan.getReturnTime().equals("")){
                updateWrapper.set("loan_return_time",loan.getReturnTime());
            }
            if (!loan.getLoanTime().equals("")){
                updateWrapper.set("loan_time",loan.getLoanTime());
            }
            loanService.update(updateWrapper);
            return new JsonResult("修改成功",new Date());
        }
        else {
            return new JsonResult(500,"修改失败",new Date());
        }
    }
    @GetMapping("/find")
    public JsonResult findLoan(String username,String bookname,String status,String startTime,String endTime){
        return new JsonResult(loanService.find(username,bookname,status,startTime,endTime));
    }
}
